package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * User: fqy
 * Date: 2021/10/15
 * Time: 16:09
 * Describe: 供应商Controller层
 */
@RestController
@RequestMapping("supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商 http://localhost:8080/supplier/list
     * @param page 当前页数
     * @param rows 每页显示的记录数
     * @param supplierName
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> getSupplierPage(Integer page, Integer rows, String supplierName){
        Map<String,Object> supplierPage = supplierService.getSupplierPage(page,rows,supplierName);
        return supplierPage;
    }

    /**
     * 供应商添加或修改
     * @param supplier
     * @param supplierId
     * @return
     */
    @PostMapping("save")
    public ServiceVO<Supplier> saveSupplier(Supplier supplier,Integer supplierId){
        // 没有id新增
        if (supplierId == null) {
            // 新增
            supplierService.insertSupplier(supplier);
        }else{
            // 修改
            supplierService.updateSupplier(supplier,supplierId);
        }
        return new ServiceVO<>(100, "保存成功");
    }

    /**
     * 供应商批量删除
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ServiceVO deleteSupplierBachByIds(String ids){
//        System.out.println("ids = " + ids);
        String[] arr = ids.split("[\\,]");
//        System.out.println(arr);
        int i = supplierService.deleteSupplierBachByIds(arr);
        if (i>=1){
            // 成功
            return new ServiceVO<>(100, "删除成功");
        }else{
            // 失败
            return new ServiceVO(200, "删除失败");
        }

    }
}
